/*
 * Ant Group
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.zmdepositdemo.controller;

import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.zmdepositdemo.model.OpenResponse;
import com.alipay.zmdepositdemo.model.TradeDeductModel;
import com.alipay.zmdepositdemo.service.trade.TradeOrderService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 扣款操作
 *
 * @author zhangjian
 * @version TradeOrderController.java, v 0.1 2024年07月03日 09:24 zhangjian
 */
@Api(tags = "扣款操作")
@ApiSupport(order = 4)
@RestController
@RequestMapping(value = "/trade-order")
public class TradeDeductController {

    @Autowired
    private TradeOrderService tradeOrderService;

    /**
     * 发起扣款
     *
     * @see
     * <a href="https://opendocs.alipay.com/open/36e05a54_alipay.trade.pay?scene=f2eb3f5d6a78455f9126fab70dd7063e&pathHash=cdbb6a87">...</a>
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "发起扣款接口")
    @PostMapping(value = "/trade-pay")
    OpenResponse<AlipayTradePayResponse> tradePay(
            @RequestBody TradeDeductModel model) {
        return tradeOrderService.tradePay(model);
    }

    /**
     * 查询扣款
     *
     * @see <a href="https://opendocs.alipay.com/open/91b5a641_alipay.trade.query?scene=common&pathHash=f3582a08">...</a>
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查询扣款接口")
    @GetMapping(value = "/query-pay")
    OpenResponse<AlipayTradeQueryResponse> queryPay(
            // （订单支付时传入的商户订单号与支付宝交易号不可同时为空）
            @ApiParam(value = "订单支付时传入的商户订单号") @RequestParam(required = false) String outTradeNo,
            @ApiParam(value = "支付宝交易号，（与商户订单号不可同时为空）", defaultValue = "2014112611001004680073956707")
            @RequestParam(required = false) String tradeNo,
            @ApiParam(value = "机构商户id（间连模式下可用，其他场景请勿使用）") @RequestParam(required = false) String orgPid,
            @ApiParam(value = "查询选项") @RequestParam(required = false) List<String> queryOptions) {
        return tradeOrderService.queryPay(outTradeNo, tradeNo, orgPid, queryOptions);
    }

    /**
     * 取消扣款
     *
     * @see <a href="https://opendocs.alipay.com/open/cdc34d41_alipay.trade.close?scene=common&pathHash=35defcd8">...</a>
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "取消扣款接口")
    @GetMapping(value = "/cancel-pay")
    OpenResponse<Boolean> cancelPay(
            // （订单支付时传入的商户订单号与支付宝交易号不可同时为空，如同时存在优先取支付宝交易号）
            @ApiParam(value = "订单支付时传入的商户订单号") @RequestParam(required = false) String outTradeNo,
            @ApiParam(value = "支付宝交易号，（与商户订单号不可同时为空）", defaultValue = "2014112611001004680073956707")
            @RequestParam(required = false) String tradeNo,
            @ApiParam(value = "商家操作员编号id，由商家自定义") @RequestParam(required = false) String operatorId) {
        return tradeOrderService.cancelPay(outTradeNo, tradeNo, operatorId);
    }

}